/*
 * @Descripttion: GGB
 * @version: 1.0
 * @Author: GGB
 * @Date: 2021-08-19 14:15:34
 * @LastEditors: GGB
 * @LastEditTime: 2021-08-19 22:27:45
 */
import 'package:flutter/material.dart';
import 'package:flutter_data_center/components/row_menu.dart';
import 'package:flutter_data_center/controller/alert_controller.dart';
import 'package:flutter_data_center/utills/color_utils.dart';
import 'package:get/get.dart';
import 'components/alert_list.dart';
import 'components/count_card.dart';

class AlertScreen extends StatefulWidget {
  const AlertScreen({Key key}) : super(key: key);
  static String routeName = "/alert";

  @override
  _AlertScreenState createState() => _AlertScreenState();
}

class _AlertScreenState extends State<AlertScreen> {
  AlertController _alertController = Get.put(AlertController());
  final keyWidget = GlobalKey();
  int currentIndex = 0;
  double height = 0;
  void calculateSizeAndPosition() =>
      WidgetsBinding.instance.addPostFrameCallback((_) {
        final RenderBox box = keyWidget.currentContext.findRenderObject();
        setState(() {
          height = box.size.height + 90;
        });
      });
  void initState() {
    super.initState();
    calculateSizeAndPosition();
    // 发起网络请求
    _alertController.getAlerts();
    _alertController.getAlertStatistics();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: buildAppBar(),
        body: SafeArea(
          child: SingleChildScrollView(
            physics: BouncingScrollPhysics(),
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 20),
              child: Column(
                children: [
                  Column(
                    key: keyWidget,
                    children: [
                      CountCard(),
                      RowMenu(
                        currentIndex: currentIndex,
                        onChange: (value) {
                          setState(() {
                            currentIndex = value;
                          });
                        },
                        activeStyle: TextStyle(
                          fontSize: 16,
                        ),
                        inactiveStyle: TextStyle(
                          fontSize: 16,
                          color: gColor("#BABFBC"),
                        ),
                        menus: _alertController.menus,
                        showRight: true,
                        showDot: false,
                        slot: Container(
                          width: 50,
                          height: 20,
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                            color: gColor("#F4F4F4"),
                            borderRadius: BorderRadius.circular(30),
                          ),
                          child: Text(
                            "历史",
                            style: TextStyle(
                              fontSize: 12,
                              height: 1,
                              color: gColor("#6B6B6B"),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                  Container(
                    height: MediaQuery.of(context).size.height - height,
                    child: AlertList(),
                  ),
                ],
              ),
            ),
          ),
        ),
      );

  AppBar buildAppBar() {
    return AppBar(
      centerTitle: true,
      backgroundColor: Colors.transparent,
      title: Text(
        "校园告警",
        style: TextStyle(
          fontSize: 18,
        ),
      ),
      elevation: 0,
    );
  }
}
